package net.xiduth.game.player.content.events;

import java.io.Serializable;

import net.xiduth.game.item.Item;
import net.xiduth.game.player.Player;
import net.xiduth.utils.Misc;

/**
 * 
 * @author Jae <jae@xiduth.com>
 * 
 * @since <Oct 18, 2013>
 *
 */
public final class SquealOfFortune implements Serializable {

	private static final long serialVersionUID = -7960916083984662554L;

	private Item[] rewards; 

	/** The instance of the player in this class. */
	private Player player;

	public SquealOfFortune(Player player) {
		this.player = player;
		player.spins = 1;
	}

	public boolean handleButtons(int... args) {
		int interfaceId = args[0];
		int buttonId = args[1];
		switch(interfaceId) {
		case 1253:
			switch(buttonId) {
			case 106:
				sendCloseScreen();
				return true;
			case 93:
				if (player.getAttributes().get("sof_item") == null && player.spins > 0)
					spinWheel();
				else
					player.sendMessage("You have either already claimed your reward or you do not have enough spins to spin the wheel...");
				return true;
			case 192:
				if (player.getAttributes().get("sof_item") != null) {
					if (player.getInventory().hasFreeSlots())
						player.getInventory().addItem((Item) player.getAttributes().get("sof_item")
								);
					else {
						player.getBank().addItem(((Item) player.getAttributes().get("sof_item")).getId(), ((Item) player.getAttributes().get("sof_item")).getAmount(), true);
						player.sendMessage("Your squeal of fortune item has been placed in your bank.");
					} player.getAttributes().remove("sof_item");
					player.getPackets().sendGlobalConfig(1790, 7);
					player.getPackets().sendConfigByFile(10861, 0);
				}
				player.getSquealOfFortune().showSpins();
				return true;
			case 273:
				sendMainScreen();
				return true;
			case 258:
				sendCloseScreen();
				return true;
			case 239:
				player.getAttributes().remove("sof_item");
				player.getPackets().sendGlobalConfig(1790, 7);
				player.getPackets().sendConfigByFile(10861, 0);
				return true;
			}
			break;
		case 1139:
			switch(buttonId) {
			case 18:
				if (player.getAttributes().get("sof_item") != null) {
					player.getPackets().sendWindowsPane(1253, 1);
					player.getPackets().sendRunScript(5906);
					return true;
				}
				if (player.spins - 1 < 0 || player.spins == 0 || player.spins < 0) {
					player.getPackets().sendGameMessage("You do not have enough spins to play Squeal of Fortune.");
					return true;
				}
				sendMainScreen();
				return true;
			case 23:
				player.getPackets().sendOpenURL("http://www.xiduth.net/new/donate/index.php");
				return true;
			}
			break;
		}
		return false;
	}

	private void sendCloseScreen() {
		player.getPackets().sendWindowsPane(player.getInterfaceManager().hasRezizableScreen() ? 746 : 548, 0);
	}

	public void showSpins() {
		player.getPackets().sendConfigByFile(10862, player.spins);
		player.getPackets().sendConfigByFile(11026, 0);
		player.getPackets().sendGlobalConfig(1800, 0);
		player.getPackets().sendGlobalConfig(823, 1);
	}

	private void spinWheel() {
		if (player.spins == 0 || player.spins - 1 < 0) return;
		player.spins--;
		showSpins();
		int random = Misc.random(rewards.length - 1);
		int[] rares = { 8, 13, 0, 4};
		for (int rare : rares) {
			if (random == rare) {
				if (Misc.random(500) < 450) {
					random = (random == 0 ? random + 1 : random - 1);
				}
			}
		}
		player.getPackets().sendConfigByFile(10860, random); // the prize won.
		player.getPackets().sendConfigByFile(10861, 1);
		player.getPackets().sendGlobalConfig(823, 1);
		player.getPackets().sendGlobalConfig(1781, 1);
		player.getPackets().sendGlobalConfig(1790, 1);
		Item reward = rewards[random];
		player.getAttributes().put("sof_item", reward);
	}

	public void setSpins(int spins) {
		this.player.spins = spins;
		showSpins();
	}

	public void sendTab(boolean fullscreen) {
		player.getInterfaceManager().sendTab(fullscreen ? 119 : 179, 1139);
		player.getPackets().sendGlobalConfig(823, 1);
		showSpins();
	}

	public int getSpins() {
		return player.spins;
	}

	public void sendMainScreen() {
		if (player.getAttributes().get("sof_item") == null) {
			player.getPackets().sendWindowsPane(1253, 1);
			switch(Misc.random(3)) {
			case 0:
				rewards = new Item[] { 
						new Item(995, 10000000),  //Rare Slot:		Coins(10M)
						new Item(23717, 1),   	  //Common Slot: 	Small Lamp - Attack(1)
						new Item(24155, 1), 	  //Uncommon Slot: 	Double Spin Ticket(1)
						new Item(995, 1000),	  //Common Slot: 	Coins(1000)
						new Item(23682, 1), 	  //Rare Slot: 		Zamorak Godsword(1)
						new Item(685, 1), 		  //Common Slot: 	Old Boot(1)
						new Item(23759, 1), 	  //Uncommon Slot: 	Large Lamp - Agility(1)
						new Item(1291, 1),  	  //Common Slot: 	Bronze Longsword(1)
						new Item(995, 20000000),	  //Rare Slot: 		Coins(20M)
						new Item(24155, 1), 	  //Uncommon Slot: 	Double Spin Ticket(1)
						new Item(23713, 1), 	  //Common Slot: 	Small Lamp(1)
						new Item(23713, 1), 	  //Common Slot: 	Small Lamp(1)
						new Item(23683, 1), 		  //Amazing Slot: 	Christmas Cracker(1)
				};
				break;
			case 1:
				rewards = new Item[] { 
						new Item(23681, 1),  	  //Rare Slot:		Saradomin Godsword(1)
						new Item(23713, 1),   	  //Common Slot: 	Small Lamp(1)
						new Item(8007, 3),        //Uncommon Slot: 	Varrock Teleport(3)
						new Item(995, 10000),	  //Common Slot: 	Coins(10000)
						new Item(23693, 1), //Rare Slot: 		Coins(100M)
						new Item(685, 1), 		  //Common Slot: 	Old Boot(1)
						new Item(23719, 1), 	  //Uncommon Slot: 	Large Lamp - Attack(1)
						new Item(995, 5000000),  	  //Common Slot: 	Coins(50)
						new Item(9470, 1),	      //Rare Slot: 		Gnome Scarf(1)
						new Item(995, 10000000), 	  //Uncommon Slot: 	Coins(250k)
						new Item(23713, 1), 	  //Common Slot: 	Small Lamp(1)
						new Item(995, 10000), 	  //Common Slot: 	Coins(10000)
						new Item(23695, 1), 		  //Amazing Slot: 	Green H'Ween Mask(1)
				};	
				break;
			case 2:
				rewards = new Item[] { 
						new Item(23690, 1),  	  //Rare Slot:		Frost Dragon Mask(1)
						new Item(995, 10000000),     //Common Slot: 	Coins(10000)
						new Item(24155, 1), 	  //Uncommon Slot: 	Double Spin Ticket(1)
						new Item(560, 250),	      //Common Slot: 	Death Rune(250)
						new Item(23691, 1), 	  	  //Rare Slot: 		Abyssal Whip(1)
						new Item(401, 1), 		  //Common Slot: 	Seaweed(1)
						new Item(995, 25000000), 	  //Uncommon Slot: 	Coins(250k)
						new Item(1197, 1),  	  //Common Slot: 	Mithril Kiteshield(1)
						new Item(995, 200000),	  //Rare Slot: 		Coins(20M)
						new Item(24155, 1), 	  //Uncommon Slot: 	Double Spin Ticket(1)
						new Item(361, 100), 	  //Common Slot: 	Tuna(100)
						new Item(23713, 1), 	  //Common Slot: 	Small Lamp(1)
						new Item(23698, 1), //Amazing Slot: 	Coins(100M)
				};
				break;
			case 3:
				rewards = new Item[] { 
						new Item(23697, 1),  //Rare Slot:		Coins(10M)
						new Item(23717, 1),   	  //Common Slot: 	Small Lamp - Attack(1)
						new Item(24155, 1), 	  //Uncommon Slot: 	Double Spin Ticket(1)
						new Item(995, 10000000),	  //Common Slot: 	Coins(1000)
						new Item(23687, 1), 	  //Rare Slot: 		Bandos Chestplate(1)
						new Item(685, 1), 		  //Common Slot: 	Old Boot(1)
						new Item(23715, 1), 	  //Uncommon Slot: 	Large Lamp(1)
						new Item(1291, 1),  	  //Common Slot: 	Bronze Longsword(1)
						new Item(23688, 1),	      //Rare Slot: 		Bandos Tassets(1)
						new Item(24155, 1), 	  //Uncommon Slot: 	Double Spin Ticket(1)
						new Item(23713, 1), 	  //Common Slot: 	Small Lamp(1)
						new Item(23733, 1), 	  //Common Slot: 	Small Lamp - Magic(1)
						new Item(11789, 1), 	  //Amazing Slot: 	Grim Reaper Mask(1)
				};
				break;
			}
			player.getPackets().sendConfigByFile(11155, 13);
			player.getPackets().sendItems(665, rewards);
		} else {
			player.getPackets().sendWindowsPane(1253, 1);
			player.getPackets().sendRunScript(5906);
		}
	}

}